/**
 * Contains functions that deal with ICMP packets.
 *
 * Specifically, receiving echo requests and sending the following error messages:
 *
 * 1. Destination unreachable (type 3, code 0): 
 *      Sent if there is a non-existent route to the destination IP (no matching entry 
 *      in routing table when forwarding an IP packet).
 * 2. Destination host unreachable (type 3, code 1): Sent if five ARP requests were sent 
 *      to the next-hop IP without a response.
 * 3. Protocol unreachable (type 3, code 2): Sent if an IP packet containing a UDP or TCP 
 *      payload is sent to one of the router's interfaces. This is needed for traceroute 
 *      to work.
 * 4. Time exceeded (type 11, code 0): Sent if an IP packet is discarded during processing 
 *      because the TTL field is 0. This is also needed for traceroute to work.
 */

#include "sr_router.h"

#define ICMP_UNREACHABLE_TYPE 3
#define ICMP_NET_UNREACHABLE_CODE 0
#define ICMP_HOST_UNREACHABLE_CODE 1
#define ICMP_PROTOCOL_UNREACHABLE_CODE 2

#define ICMP_TIME_EXCEEDED_TYPE 11
#define ICMP_TIME_EXCEEDED_CODE 0

#define ICMP_TTL 64

/**
 * If packet is echo request:
 *      handle_echo_request
 * else:
 *      do nothing
 */
void handle_received_icmp_packet(struct sr_instance* sr, 
                             uint8_t * packet/* lent */,
                             unsigned int len);

/**
 * Sends ICMP echo reply to requesting host
 */
void handle_echo_request();

// ICMP echo reply ICMP header
// 
//  0                   1                   2                   3
//  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |     Type      |     Code      |          Checksum             |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |           Identifier          |        Sequence Number        |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |     Data ...
// +-+-+-+-+-


// Destination Unreachable ICMP packet
//
//  0                   1                   2                   3
//  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |     Type      |     Code      |          Checksum             |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |                             unused                            |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |      Internet Header + 64 bits of Original Data Datagram      |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+